<?php

namespace App\Repositories;

use App\Models\AmountDetail;
use App\Models\User;
use App\Exceptions\InvalidException;
use Illuminate\Support\Facades\Log;

class AmountRepo
{
	public static function genneral(User $user,string $type,float $amount,int $action=1,int $order_id=0,int $status=1)
	{
		//执行之前
		$before = self::getAmountType($type,$user);
		//执行之后
		$after  = $action == 1 ? $before+$amount : $before-$amount;
        Log::info('before:'.$before.';after:'.$after);
		//创建记录
		AmountDetail::create([
			'user_id' => $user->id,
			'type'    => $type,
			'amount'  => $amount,
			'before'  => $before,
			'after'   => $after,
			'action'  => $action,
			'status'  => $status,
			'order_id'=> $order_id,
			'created_at' => date('Y-m-d H:i:s')
		]);
	}

	public static function getAmountType(string $type,User $user) {
		if($type == 'money') return $user->money;
		if($type == 'brokerage') return $user->brokerage;
        if($type == 'integral')  return $user->integral;
	}

}
